/*   1:    */ package org.jetbrains.generate.tostring;
/*   2:    */ 
/*   3:    */

import com.intellij.codeInsight.generation.PsiElementClassMember;
import com.intellij.codeInsight.hint.HintManager;
import com.intellij.ide.util.MemberChooser;
import com.intellij.ide.util.MemberChooserBuilder;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.actionSystem.EditorWriteActionHandler;
import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.options.ShowSettingsUtil;
import com.intellij.openapi.options.TabbedConfigurable;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.ComboBox;
import com.intellij.openapi.util.Disposer;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.generate.tostring.config.Config;
import org.jetbrains.generate.tostring.template.TemplateResource;
import org.jetbrains.generate.tostring.template.TemplatesManager;
import org.jetbrains.generate.tostring.view.TemplatesPanel;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/*   4:    */
/*   5:    */
/*   6:    */
/*   7:    */
/*   8:    */
/*   9:    */
/*  10:    */
/*  11:    */
/*  12:    */
/*  13:    */
/*  14:    */
/*  15:    */
/*  16:    */
/*  17:    */
/*  18:    */
/*  19:    */
/*  20:    */
/*  21:    */
/*  22:    */
/*  23:    */
/*  24:    */
/*  25:    */
/*  26:    */
/*  27:    */
/*  28:    */
/*  29:    */
/*  30:    */
/*  31:    */
/*  32:    */
/*  33:    */
/*  34:    */
/*  35:    */
/*  36:    */
/*  37:    */
/*  38:    */
/*  39:    */
/*  40:    */
/*  41:    */
/*  42:    */
/*  43:    */
/*  44:    */
/*  45:    */
/*  46:    */
/*  47:    */ 
/*  48:    */ public class GenerateToStringActionHandlerImpl
/*  49:    */   extends EditorWriteActionHandler
/*  50:    */   implements GenerateToStringActionHandler
/*  51:    */ {
/*  52: 60 */   private static final Logger a = Logger.getInstance("#org.jetbrains.generate.tostring.GenerateToStringActionHandlerImpl");
/*  53:    */   
/*  54:    */   public void executeWriteAction(Editor paramEditor, DataContext paramDataContext)
/*  55:    */   {
/*  56: 63 */     Project localProject = (Project)CommonDataKeys.PROJECT.getData(paramDataContext);
/*  57: 64 */     assert (localProject != null);
/*  58:    */     
/*  59: 66 */     PsiClass localPsiClass = a(paramEditor, paramDataContext);
/*  60: 67 */     assert (localPsiClass != null);
/*  61:    */     
/*  62: 69 */     a(localProject, localPsiClass, paramEditor);
/*  63:    */   }
/*  64:    */   
/*  65:    */   public void executeActionQuickFix(Project paramProject, PsiClass paramPsiClass)
/*  66:    */   {
/*  67: 74 */     a(paramProject, paramPsiClass, null);
/*  68:    */   }
/*  69:    */   
/*  70:    */   private static void a(@NotNull Project paramProject, @NotNull final PsiClass paramPsiClass, final Editor paramEditor)
/*  71:    */   {
/*  72: 84 */     if (paramProject == null) {
/*  73: 84 */       throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[] { "0", "org/jetbrains/generate/tostring/GenerateToStringActionHandlerImpl", "doExecuteAction" }));
/*  74:    */     }
/*  75: 84 */     if (paramPsiClass == null) {
/*  76: 84 */       throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[] { "1", "org/jetbrains/generate/tostring/GenerateToStringActionHandlerImpl", "doExecuteAction" }));
/*  77:    */     }
/*  78: 84 */     a.debug("+++ doExecuteAction - START +++");
/*  79: 86 */     if (a.isDebugEnabled()) {
/*  80: 87 */       a.debug("Current project " + paramProject.getName());
/*  81:    */     }
/*  82: 90 */     final PsiElementClassMember[] arrayOfPsiElementClassMember = a(paramPsiClass);
/*  83: 91 */     if (arrayOfPsiElementClassMember.length == 0)
/*  84:    */     {
/*  85: 92 */       HintManager.getInstance().showErrorHint(paramEditor, "No members to include in toString() have been found");
/*  86: 93 */       return;
/*  87:    */     }
/*  88: 96 */     final MemberChooserBuilder localMemberChooserBuilder = new MemberChooserBuilder(paramProject);
/*  89: 97 */     final MemberChooserHeaderPanel localMemberChooserHeaderPanel = new MemberChooserHeaderPanel(paramPsiClass);
/*  90: 98 */     localMemberChooserBuilder.setHeaderPanel(localMemberChooserHeaderPanel);
/*  91: 99 */     localMemberChooserBuilder.overrideAnnotationVisible(PsiUtil.isLanguageLevel5OrHigher(paramPsiClass));
/*  92:100 */     localMemberChooserBuilder.setTitle("Generate toString()");
/*  93:    */     
/*  94:102 */     a.debug("Displaying member chooser dialog");
/*  95:103 */     ApplicationManager.getApplication().invokeLater(new Runnable()
/*  96:    */     {
/*  97:    */       public void run()
/*  98:    */       {
/*  99:105 */         if (this.val$project.isDisposed()) {
/* 100:105 */           return;
/* 101:    */         }
/* 102:106 */         MemberChooser localMemberChooser = localMemberChooserBuilder.createBuilder(arrayOfPsiElementClassMember);
/* 103:107 */         localMemberChooser.setCopyJavadocVisible(false);
/* 104:108 */         localMemberChooser.selectElements(arrayOfPsiElementClassMember);
/* 105:109 */         localMemberChooserHeaderPanel.setChooser(localMemberChooser);
/* 106:110 */         localMemberChooser.show();
/* 107:112 */         if (0 == localMemberChooser.getExitCode())
/* 108:    */         {
/* 109:113 */           List localList = GenerateToStringUtils.convertClassMembersToPsiMembers(localMemberChooser.getSelectedElements());
/* 110:    */           
/* 111:115 */           TemplateResource localTemplateResource = localMemberChooserHeaderPanel.getSelectedTemplate();
/* 112:116 */           TemplatesManager.getInstance().setDefaultTemplate(localTemplateResource);
/* 113:118 */           if (localTemplateResource.isValidTemplate()) {
/* 114:119 */             GenerateToStringWorker.executeGenerateActionLater(paramPsiClass, paramEditor, localList, localTemplateResource, localMemberChooser.isInsertOverrideAnnotation());
/* 115:    */           } else {
/* 116:123 */             HintManager.getInstance().showErrorHint(paramEditor, "toString() template '" + localTemplateResource.getFileName() + "' is invalid");
/* 117:    */           }
/* 118:    */         }
/* 119:    */       }
/* 120:128 */     });
/* 121:129 */     a.debug("+++ doExecuteAction - END +++");
/* 122:    */   }
/* 123:    */   
/* 124:    */   public static void updateDialog(PsiClass paramPsiClass, MemberChooser<PsiElementClassMember> paramMemberChooser)
/* 125:    */   {
/* 126:133 */     PsiElementClassMember[] arrayOfPsiElementClassMember = a(paramPsiClass);
/* 127:134 */     paramMemberChooser.resetElements(arrayOfPsiElementClassMember);
/* 128:135 */     paramMemberChooser.selectElements(arrayOfPsiElementClassMember);
/* 129:    */   }
/* 130:    */   
/* 131:    */   private static PsiElementClassMember[] a(PsiClass paramPsiClass)
/* 132:    */   {
/* 133:139 */     Config localConfig = GenerateToStringContext.getConfig();
/* 134:140 */     PsiField[] arrayOfPsiField = GenerateToStringUtils.filterAvailableFields(paramPsiClass, localConfig.getFilterPattern());
/* 135:141 */     if (a.isDebugEnabled()) {
/* 136:141 */       a.debug("Number of fields after filtering: " + arrayOfPsiField.length);
/* 137:    */     }
/* 138:    */     PsiMethod[] arrayOfPsiMethod;
/* 139:143 */     if (localConfig.enableMethods)
/* 140:    */     {
/* 141:145 */       arrayOfPsiMethod = GenerateToStringUtils.filterAvailableMethods(paramPsiClass, localConfig.getFilterPattern());
/* 142:146 */       if (a.isDebugEnabled()) {
/* 143:146 */         a.debug("Number of methods after filtering: " + arrayOfPsiMethod.length);
/* 144:    */       }
/* 145:    */     }
/* 146:    */     else
/* 147:    */     {
/* 148:148 */       arrayOfPsiMethod = PsiMethod.EMPTY_ARRAY;
/* 149:    */     }
/* 150:151 */     return GenerateToStringUtils.combineToClassMemberList(arrayOfPsiField, arrayOfPsiMethod);
/* 151:    */   }
/* 152:    */   
/* 153:    */   public boolean isEnabled(Editor paramEditor, DataContext paramDataContext)
/* 154:    */   {
/* 155:156 */     return a(paramEditor, paramDataContext) != null;
/* 156:    */   }
/* 157:    */   
/* 158:    */   @Nullable
/* 159:    */   private static PsiClass a(Editor paramEditor, DataContext paramDataContext)
/* 160:    */   {
/* 161:161 */     PsiFile localPsiFile = (PsiFile)CommonDataKeys.PSI_FILE.getData(paramDataContext);
/* 162:162 */     if (localPsiFile == null) {
/* 163:162 */       return null;
/* 164:    */     }
/* 165:164 */     int i = paramEditor.getCaretModel().getOffset();
/* 166:165 */     PsiElement localPsiElement = localPsiFile.findElementAt(i);
/* 167:167 */     if (localPsiElement == null) {
/* 168:167 */       return null;
/* 169:    */     }
/* 170:169 */     PsiClass localPsiClass = (PsiClass)PsiTreeUtil.getParentOfType(localPsiElement, PsiClass.class, false);
/* 171:170 */     if (localPsiClass == null) {
/* 172:171 */       return null;
/* 173:    */     }
/* 174:175 */     return localPsiClass.isInterface() ? null : localPsiClass;
/* 175:    */   }
/* 176:    */   
/* 177:    */   public static class MemberChooserHeaderPanel
/* 178:    */     extends JPanel
/* 179:    */   {
/* 180:    */     private MemberChooser a;
/* 181:    */     private final JComboBox b;
/* 182:    */     
/* 183:    */     public void setChooser(MemberChooser paramMemberChooser)
/* 184:    */     {
/* 185:183 */       this.a = paramMemberChooser;
/* 186:    */     }
/* 187:    */     
/* 188:    */     public MemberChooserHeaderPanel(final PsiClass paramPsiClass)
/* 189:    */     {
/* 190:187 */       super();
/* 191:    */       
/* 192:189 */       Collection localCollection = TemplatesManager.getInstance().getAllTemplates();
/* 193:190 */       TemplateResource[] arrayOfTemplateResource = (TemplateResource[])localCollection.toArray(new TemplateResource[localCollection.size()]);
/* 194:    */       
/* 195:192 */       JButton localJButton = new JButton("Settings");
/* 196:193 */       localJButton.setMnemonic(83);
/* 197:    */       
/* 198:195 */       this.b = new ComboBox(arrayOfTemplateResource);
/* 199:196 */       localJButton.addActionListener(new ActionListener()
/* 200:    */       {
/* 201:    */         public void actionPerformed(ActionEvent paramAnonymousActionEvent)
/* 202:    */         {
/* 203:198 */           final TemplatesPanel localTemplatesPanel = new TemplatesPanel();
/* 204:199 */           Disposable localDisposable = Disposer.newDisposable();
/* 205:200 */           TabbedConfigurable local1 = new TabbedConfigurable(localDisposable)
/* 206:    */           {
/* 207:    */             protected List<Configurable> createConfigurables()
/* 208:    */             {
/* 209:202 */               ArrayList localArrayList = new ArrayList();
/* 210:203 */               localArrayList.add(new GenerateToStringConfigurable(GenerateToStringActionHandlerImpl.MemberChooserHeaderPanel.1.this.val$clazz.getProject()));
/* 211:204 */               localArrayList.add(localTemplatesPanel);
/* 212:205 */               return localArrayList;
/* 213:    */             }
/* 214:    */             
/* 215:    */             public String getDisplayName()
/* 216:    */             {
/* 217:209 */               return "toString() Generation Settings";
/* 218:    */             }
/* 219:    */             
/* 220:    */             public String getHelpTopic()
/* 221:    */             {
/* 222:213 */               return null;
/* 223:    */             }
/* 224:    */             
/* 225:    */             public void apply()
/* 226:    */               throws ConfigurationException
/* 227:    */             {
/* 228:218 */               super.apply();
/* 229:219 */               GenerateToStringActionHandlerImpl.updateDialog(GenerateToStringActionHandlerImpl.MemberChooserHeaderPanel.1.this.val$clazz, GenerateToStringActionHandlerImpl.MemberChooserHeaderPanel.this.a);
/* 230:    */               
/* 231:221 */               GenerateToStringActionHandlerImpl.MemberChooserHeaderPanel.this.b.removeAllItems();
/* 232:222 */               for (TemplateResource localTemplateResource : TemplatesManager.getInstance().getAllTemplates()) {
/* 233:223 */                 GenerateToStringActionHandlerImpl.MemberChooserHeaderPanel.this.b.addItem(localTemplateResource);
/* 234:    */               }
/* 235:225 */               GenerateToStringActionHandlerImpl.MemberChooserHeaderPanel.this.b.setSelectedItem(TemplatesManager.getInstance().getDefaultTemplate());
/* 236:    */             }
/* 237:228 */           };
/* 238:229 */           ShowSettingsUtil.getInstance().editConfigurable(GenerateToStringActionHandlerImpl.MemberChooserHeaderPanel.this, local1, new Runnable()
/* 239:    */           {
/* 240:    */             public void run()
/* 241:    */             {
/* 242:231 */               localTemplatesPanel.selectItem(TemplatesManager.getInstance().getDefaultTemplate());
/* 243:    */             }
/* 244:233 */           });
/* 245:234 */           Disposer.dispose(localDisposable);
/* 246:    */         }
/* 247:237 */       });
/* 248:238 */       add(localJButton, "East");
/* 249:239 */       add(this.b, "Center");
/* 250:240 */       this.b.setSelectedItem(TemplatesManager.getInstance().getDefaultTemplate());
/* 251:    */       
/* 252:242 */       JLabel localJLabel = new JLabel("Template: ");
/* 253:243 */       localJLabel.setDisplayedMnemonic('T');
/* 254:244 */       localJLabel.setLabelFor(this.b);
/* 255:    */       
/* 256:246 */       add(localJLabel, "West");
/* 257:    */     }
/* 258:    */     
/* 259:    */     public TemplateResource getSelectedTemplate()
/* 260:    */     {
/* 261:250 */       return (TemplateResource)this.b.getSelectedItem();
/* 262:    */     }
/* 263:    */   }
/* 264:    */ }



/* Location:           E:\idea\

 * Qualified Name:     org.jetbrains.generate.tostring.GenerateToStringActionHandlerImpl

 * JD-Core Version:    0.7.0.1

 */